package com.dy.design_pattern_study.test;

import com.dy.design_pattern_study.store_cashier_system.CashContextAndFactory;
import com.dy.design_pattern_study.store_cashier_system.CashFactory;
import com.dy.design_pattern_study.test.impl.Addition;
import com.dy.design_pattern_study.test.impl.Division;
import com.dy.design_pattern_study.test.impl.Multiplication;
import com.dy.design_pattern_study.test.impl.Subtraction;
import com.sun.xml.internal.ws.util.StringUtils;
import org.mockito.internal.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-03-09
 * @Content 实现一个计算加减混合运算表达式功能的程序，
 * 参考用例1：输入表达式为128+6-29，输出为105；
 * 用例2：输入表达式为-6-29，输出为-35；
 * 用例3：输入表达式为128+6+，输出为表达式错误无法计算。
 */
public class Compute extends AlgorithmStrategy {

    AlgorithmFactory algorithmFactory;
    boolean computeStatus = false;

    /**
     * @param formula 计算表达式
     */
    public void ComputeData(String formula) {

        //存储数值
        List<Double> numList = new ArrayList<>();
        StringBuffer numStr = new StringBuffer();

        //获取运算符号
        List<String> operationalSymbolList = new ArrayList<>();

        //负号
        String minusSign = null;

        Compute compute;

        //结果
        double computeResult = 0.0;

        /*
         * 这里的话简单点，
         * 直接从字符串中遍历对应的数值，并存储至 numList 集合中
         * 如果是 运算符号就存储 至 operationalSymbolList 集合中
         */
        for (int i = 0; i < formula.length(); i++) {
            char c = formula.charAt(i);
            if (c == '+' && (i == formula.length() - 1 || i == 0)) {
                System.out.println("表达式错误无法计算!");
                //设置不能计算跳过
                computeStatus = true;
                return;
            }
            if (c == '-' && (i == 0)) {
                minusSign = "-";
            }

            switch (c) {
                case '+':
                case '-':
                case '*':
                case '/':
                    if (minusSign == null) {
                        operationalSymbolList.add(c + "");
                    } else {
                        //把该负号进行赋值
                        numStr.insert(0, minusSign);
                        System.out.println("numStr = " + numStr);
                        minusSign = null;
                        break;
                    }
                    numList.add(Double.valueOf(numStr.toString()));
                    numStr.setLength(0);
                    break;
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '0':
                case '.':
                    //刚开始的时候
                    numStr.append(c + "");
                    break;
            }
        }

        if (numStr.length() > 0) {
            numList.add(Double.valueOf(numStr.toString()));
            numStr.setLength(0);
        }

        int computeCount = 0;
        //计算
        for (Double num : numList) {
            if (computeResult == 0) {
                computeResult = num;
            } else {
                compute = new Compute(operationalSymbolList.get(computeCount));
                computeResult = compute.AlgorithmInterface(computeResult, num);
                computeCount++;
            }
        }

        System.out.println("computeResult = " + computeResult);

    }

    public Compute() {
    }

    public Compute(String type) {

        switch (type) {
            case "+":
                algorithmFactory = new Addition();
                break;
            case "-":
                algorithmFactory = new Subtraction();
                break;
            case "*":
                algorithmFactory = new Multiplication();
                break;
            case "/":
                algorithmFactory = new Division();
                break;
        }
    }

    @Override
    public double AlgorithmInterface(double first, double second) {
        return algorithmFactory.FactoryInterface(first, second);
    }

    public static void main(String[] args) {
        Compute compute = null;

        System.out.println("*********** 欢迎进入 计算器 加减混合运算 ***********");

        //System.out.println("1." + "加减混合运算");

        compute = new Compute();
        //compute.ComputeData("128+6-29");
        //-6-29 128+6+
        //compute.ComputeData("128+6-29+1-1-1+1");
        //compute.ComputeData("-6-29");
        //Scanner scanner = new Scanner(System.in);
        //String nextLine = scanner.nextLine();
        //compute.ComputeData(nextLine);
        compute.compute2();

    }

    public void compute2() {
        //StringBuffer stringBuffer = new StringBuffer("-6+1-1-1");
        StringBuffer stringBuffer = new StringBuffer("128+6-29");
        if ("-".equals(stringBuffer.substring(0, 1)) || "+".equals(stringBuffer.substring(0, 1))) {
            stringBuffer.insert(0, '0');
        }

        int count = 0;
        String expressString = stringBuffer.toString();
        String[] num = expressString.split("-|\\+");

        int sum = Integer.parseInt(num[count++]);

        for (int i : expressString.toCharArray()) {
            if (i == 45) {
                sum -= Integer.parseInt(num[count++]);
            }

            if (i == 43) {
                sum += Integer.parseInt(num[count++]);
            }
        }
        System.err.println(sum);
    }
}
